package com.webcohesion.enunciate.modules.docs;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author wangfeng
 * @time 2017/10/31 13:17.
 */
public class DocUtil {
    private static final String LINE_BREAK = "\n";
    private static final String[] SIGNS = {"<br>", "</br>", "<p>", "</p>", "<h>", "</h>"};

    /**
     * 截取javadoc描述的第一行内容
     */
    public static String subDescription(String description) {
        if (description == null || Objects.equals(description.trim(), "")) {
            return description;
        }
        int index;
        if (description.contains(LINE_BREAK)) {
            index = description.indexOf(LINE_BREAK);
        } else {
            index = findFirstSignIndex(description);
        }
        if (index >= 0) {
            return description.substring(0, index);
        }
        return description;
    }

    private static int findFirstSignIndex(String description) {
        return Arrays.stream(SIGNS).mapToInt(description::indexOf)
                .filter(i -> i > 0)
                .sorted()
                .findFirst().orElse(-1);
    }
}
